package com.sample.springbatch.job.sample.config;

import com.sample.springbatch.job.config.BaseConfig;
import com.sample.springbatch.job.sample.processor.UserItemProcessor;
import com.sample.springbatch.job.sample.reader.UserJpaPagingItemReader;
import com.sample.springbatch.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

/**
 * @author zwg
 * @date 2019-05-11 10:59
 **/

public class UserConfig extends BaseConfig {

    Logger logger = LoggerFactory.getLogger(UserConfig.class);

    @Autowired
    UserJpaPagingItemReader userJpaPagingItemReader;

    @Bean(name = "userJob")
    public Job importUserJob() {
        return jobBuilderFactory.get("userJob")
                .incrementer(new RunIdIncrementer())
                .flow(userStep())
                .end()
                .build();
    }

    @Bean
    public Step userStep() {
        return stepBuilderFactory.get("userStep")
                .<User, User> chunk(1)
                .reader(userJpaPagingItemReader)
                .processor(processor())
                .build();
    }

    @Bean
    public ItemProcessor processor() {
        return new ItemProcessor<User,User>() {

            @Override
            public User process(User item) throws Exception {
                item.setAge(item.getAge()+1);
                logger.info("user:{}",item);
                return item;
            }
        };
    }


}
